<%@page import="edu.tsinghua.eea.powermanagement.adapters.DBPlugAdapter.CFLAG"%>
<%@page import="edu.tsinghua.eea.powermanagement.adapters.DBPlugAdapter.SFLAG"%>
<%@page import="edu.tsinghua.eea.powermanagement.data.AppInfoCell"%>
<%@page import="java.util.Vector"%>
<%@page import="edu.tsinghua.eea.powermanagementserver.model.DBInterface"%>
<%@page import="edu.tsinghua.eea.powermanagement.data.PlugInfoCell"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="edu.tsinghua.eea.powermanagement.data.HomeInfo"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
</style>
<link href="css/rf_style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
	<div style="background: #FFFFFF;">
		<div id="xsnazzy">
			<b class="xtop">
				<b class="xb1"></b><b class="xb2 gray_back"></b><b class="xb3 gray_back"></b>
				<b class="xb4 gray_back"></b><b class="xb5 gray_back"></b>
			</b>
			<div class="xboxcontent">
				<div class="top_title">
					插座及电器设置
				</div>
				<jsp:include page="server_message.jsp"></jsp:include>
				<div class='fleft inner_title'>
					添加插座和电器
				</div>
				<div class='fleft wfill'>	
					<div class='fleft'>
						添加插座：
						<div class='next_level'>
							<form id='add_plug' method='get' action='dbcon'>
								<input name='action' value='<%=DBInterface.ACTION_ADD_PLUG%>' type='hidden' />
								<div>
									<div class='plug_label'>请输入插座MAC地址：</div>
									<div class='plug'>
										<input name='mac' type='text' class='plug' />
									</div>
									<div class='clear'></div>
								</div>
								<div>
									<div class='plug_label'>请输入对应电器ID：</div>
									<div class='plug'>
										<input name='EID' type='text' class='plug' value='0'/>
									</div>
									<div class='clear'></div>
								</div>
								<div>
									<input type='submit' value='添加插座' class='smaller_font' />
								</div>
							</form>
						</div>
					</div>
					<div class='vgapsep rbar'></div>
					<div class='vgapsep'></div>
					<div class='fleft'>
						添加电器：
						<div class='next_level'>
							<form id='add_app' method='get' action='dbcon'>
								<input name='action' value='<%=DBInterface.ACTION_ADD_APP%>' type='hidden' />
								<div>
									<div class='plug_label'>请输入电器名称：</div>
									<div class='plug'>
										<input name='app_name' type='text' class='plug' />
									</div>
									<div class='clear'></div>
								</div>
								<div>
									<div class='plug_label'>请输入电器类型ID：</div>
									<div class='plug'>
										<input name='etid' type='text' class='plug' value='0'/>
									</div>
									<div class='clear'></div>
								</div>
								<div>
									<input type='submit' value='添加电器' class='smaller_font' />
								</div>
							</form>
						</div>
					</div>
					<div class='clear'></div>
				</div><%
					Vector<PlugInfoCell> va = (Vector<PlugInfoCell>) request
							.getSession().getAttribute("plugs");
					Vector<AppInfoCell> vb = (Vector<AppInfoCell>) request.getSession()
							.getAttribute("apps");
					boolean bPlugTableEnabled = false;
					boolean bAppTableEnabled = false;
					if (va != null && va.size() > 0)
						bPlugTableEnabled = true;
					if (vb != null && vb.size() > 0)
						bAppTableEnabled = true;
				%>
				<%
					if (bPlugTableEnabled || bAppTableEnabled) {
						// 存在插座时显示
				%>
				<div class='fleft inner_title'>
					插座信息表
				</div><%
					}
					if (bPlugTableEnabled) {
				%>
				<div class='fleft' style='margin-right:24px;'>
					<div>插座信息</div>
					<div class=''>
						<table class='plug'>
							<tbody>
								<tr class='tab_row1'>
									<td class='normal_border'>插座MAC地址</td>
									<td class='normal_border'>插座电器ID</td>
									<td class='normal_border'>开关状态</td>
									<td class='normal_border'>编辑选项</td>
								</tr><%
									for (int i = 0; i < va.size(); i++) {
								%>
								<tr class='tr<%=i % 2%>'>
									<td class='normal_border'><%=va.get(i).mMAC%></td>
									<td class='normal_border'><%=va.get(i).mEID%></td>
									<td class='normal_border'><div>
									<%
										SFLAG sf = va.get(i).mSFlag;
												CFLAG cf = va.get(i).mCFlag;
												if (sf == SFLAG.Open) {
									%><div class="fleft small_ico bggreen"></div><%
										} else if (sf == SFLAG.Close) {
									%><div class="fleft small_ico bgblack"></div><%
										} else {
									%><div class="fleft small_ico bgred"></div><%
										}
									%>
										<div class="fleft">
												<%
													if (cf == CFLAG.Open) {
												%>正在开启<%
													} else if (cf == CFLAG.Close) {
												%>正在关闭<%
													} else if (sf == SFLAG.Open) {
												%>已开启<%
													} else if (sf == SFLAG.Close) {
												%>已关闭<%
													} else {
												%>设备未连接<%
													}
												%></div>
										<div class="clear"></div>
									</div></td>
									<td class='normal_border'>
<%-- 										编辑选项<%=va.get(i).mID %> --%>
										<a class='plug' href='dbcon?action=<%=DBInterface.ACTION_PLUG_STATE%>&id=<%=va.get(i).mID%>&state=<%=CFLAG.Open.ordinal()%>'>开</a>
										<a class='plug' href='dbcon?action=<%=DBInterface.ACTION_PLUG_STATE%>&id=<%=va.get(i).mID%>&state=<%=CFLAG.Close.ordinal()%>'>关</a>
										<a class='plug' href='dbcon?action=<%=DBInterface.ACTION_DELETE_PLUG%>&id=<%=va.get(i).mID%>'>删除</a>
									</td>
								</tr>
								<%
									}
								%>
							</tbody>
						</table>
						<div id="plug_legend">
							<div class="fleft">开关状态：</div>
							<div class="fleft small_ico bggreen"></div>
							<div class="fleft">开启</div>
							<div class="fleft small_ico bgblack"></div>
							<div class="fleft">关闭</div>
							<div class="fleft small_ico bgred"></div>
							<div class="fleft">未知</div>
							<div class="clear"></div>
						</div>
					</div>
				</div><%
					}
					if (bAppTableEnabled) {
						// 存在电器时显示
				%>
				<div class='fleft'>
					<div>电器信息</div>
					<div class=''>
						<table class='plug'>
							<tbody>
								<tr class='tab_row1'>
									<td class='normal_border'>电器名称</td>
									<td class='normal_border'>编辑选项</td>
								</tr><%
									for (int i = 0; i < vb.size(); i++) {
								%>
								<tr class='tr<%=i % 2%>'>
									<td class='normal_border'><%=vb.get(i).mName%></td>
									<td class='normal_border'>
<%-- 										编辑选项<%=va.get(i).mID %> --%>
										<a class='plug' href='dbcon?action=<%=DBInterface.ACTION_DELETE_APP%>&id=<%=vb.get(i).mID%>'>删除</a>
									</td>
								</tr>
								<%
									}
								%>
							</tbody>
						</table>
					</div>
				</div><%
					}
				%>
				<div class='clear'></div>
			</div>
		</div>
		<b class="xbottom">
			<b class="xb5"></b><b class="xb5"></b><b class="xb4"></b>
			<b class="xb3"></b><b class="xb2"></b><b class="xb1"></b>
		</b>
	</div>
</body>
</html>